package com.whlilong.jxc.storagemanager.editor;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Text;

import com.swtdesigner.ResourceManager;
import com.whlilong.jxc.abstracteditor.AbstractEditor;
import com.whlilong.jxc.db.DbManager;
import com.whlilong.jxc.listener.IEditorListener;
import com.whlilong.jxc.model.Iostorage;
import com.whlilong.jxc.model.Storage;
import com.whlilong.jxc.print.IconSource;
import com.whlilong.jxc.print.PDocument;
import com.whlilong.jxc.print.PHLine;
import com.whlilong.jxc.print.PTableBoxProvider;
import com.whlilong.jxc.print.PTextBox;
import com.whlilong.jxc.print.PVSpace;
import com.whlilong.jxc.print.PrintPreview;
import com.whlilong.jxc.print.SWTPTable;
import com.whlilong.jxc.sys.ExtentionInfo;
import com.whlilong.jxc.sys.SetTableColColorListener;
import com.whlilong.jxc.ui.provider.IostorageTableContentProvider;
import com.whlilong.jxc.ui.provider.IostorageTableLableProvider;
import com.whlilong.jxc.ui.provider.StorageTableContentProvider;
import com.whlilong.jxc.ui.sorter.DistrictSorter;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class StorageManagerEditor extends AbstractEditor implements
		IEditorListener {

	private Composite container;
	private CTabFolder tabFolder;
	private CTabItem tabItem;
	private CTabItem tabItem_1;
	private Composite composite;
	private Text textcode;
	private Text text_goodsname;
	private Table table;
	private Composite composite_1;
	private Label label_2;
	private Text text_2;
	private Label label_3;
	private Text text_3;
	private Button button_1;
	private Table table_1;
	private TableViewer tableViewer;
	private Composite composite_2;
	private Label labelgoodsid;
	private Text textgoodsid;
	private Label labelbarcode;
	private Text textbarcode;
	private Button buttonsearch;
	private Label label;
	private Text text_goodsid;
	private Label label_1;
	private Text textgoodsname;
	private Button button;
	private TableViewer iotableViewer;
	private Table iotable;

	public StorageManagerEditor() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @wbp.parser.entryPoint
	 */
	public Control createGroupText(Composite parent) {

		container = new Composite(parent, SWT.NONE);
		final GridLayout gl_container = new GridLayout(1, false);
		container.setLayout(gl_container);

		createBook(container);
//		initial();
//		setInput();
		addListener();
		return container;

	}
	private void addListener() {
		// TODO 监听查询操作
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String goodsid = text_goodsid.getText();
				String goodsname = text_goodsname.getText();
				String barcode = textcode.getText();
				
				StringBuffer hql = new StringBuffer("from Storage where 1=1");
				List paramlist = new ArrayList();
				List valuelist = new ArrayList();
				int count = 0;
				if(!goodsid.equals("")){
					hql.append(" and goodsid =?");
					paramlist.add(count);
					valuelist.add(goodsid);
					count++;
				}
				if(!goodsname.equals("")){
					hql.append(" and goodsname =?");
					paramlist.add(count);
					valuelist.add(goodsname);
					count++;
				}
				if(!barcode.equals("")){
					hql.append(" and barcode =?");
					paramlist.add(count);
					valuelist.add(barcode);
					count++;
				}
				Integer[] param = new Integer[paramlist.size()];
				Object[] values = new Object[valuelist.size()];
				values = valuelist.toArray(values);
				param = (Integer[]) paramlist.toArray(param);
				List<Storage> list = DbManager.getInstance().search(hql.toString(),param, values);
				tableViewer.setInput(list);
			}
		});
		
		buttonsearch.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String goodsid = textgoodsid.getText();
				String goodsname = textgoodsname.getText();
				String barcode = textbarcode.getText();
				
				StringBuffer hql = new StringBuffer("from Iostorage where 1=1");
				List paramlist = new ArrayList();
				List valuelist = new ArrayList();
				int count = 0;
				if(!goodsid.equals("")){
					hql.append(" and goodsid =?");
					paramlist.add(count);
					valuelist.add(goodsid);
					count++;
				}
				if(!goodsname.equals("")){
					hql.append(" and goodsname =?");
					paramlist.add(count);
					valuelist.add(goodsname);
					count++;
				}
				if(!barcode.equals("")){
					hql.append(" and barcode =?");
					paramlist.add(count);
					valuelist.add(barcode);
					count++;
				}
				Integer[] param = new Integer[paramlist.size()];
				Object[] values = new Object[valuelist.size()];
				values = valuelist.toArray(values);
				param = (Integer[]) paramlist.toArray(param);
				List<Iostorage> list = DbManager.getInstance().search(hql.toString(),param, values);
				iotableViewer.setInput(list);
			}
		});
	}

	private void createBook(Composite parent) {
		tabFolder = new CTabFolder(parent, SWT.BORDER);
		tabFolder.setSimple(false);
		tabFolder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1,
				1));
		tabFolder.setSelectionBackground(Display.getCurrent().getSystemColor(
				SWT.COLOR_TITLE_INACTIVE_BACKGROUND_GRADIENT));

		tabItem = new CTabItem(tabFolder, SWT.NONE);
		tabItem.setText("库存管理");
		tabItem.setImage(ResourceManager.getPluginImage("com.whlilong.jxc",
				"icons/brand1.gif"));
		
		composite = new Composite(tabFolder, SWT.NONE);
		tabItem.setControl(composite);
		composite.setLayout(new GridLayout(7, false));
		
		Label labelcode = new Label(composite, SWT.NONE);
		labelcode.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		labelcode.setText("商品条码：");
		
		textcode = new Text(composite, SWT.BORDER);
		textcode.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		
		label = new Label(composite, SWT.NONE);
		label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label.setText("商品编号：");
		
		text_goodsid = new Text(composite, SWT.BORDER);
		text_goodsid.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		
		Label label_goodsname = new Label(composite, SWT.NONE);
		label_goodsname.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_goodsname.setText("商品名称：");
		
		text_goodsname = new Text(composite, SWT.BORDER);
		text_goodsname.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		
		button = new Button(composite, SWT.NONE);
		
		GridData gd_button = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_button.widthHint = 80;
		button.setLayoutData(gd_button);
		button.setText("查询");
		
		tableViewer = new TableViewer(composite, SWT.BORDER | SWT.FULL_SELECTION);
		tableViewer.setUseHashlookup(true);
		table = tableViewer.getTable();
		table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 7, 1));
		table.setToolTipText("点击单元格对数据进行修改！");
		// 设置行高
		table.addListener(SWT.MeasureItem, new Listener() {
			public void handleEvent(Event event) {

				event.height = 20;
			}
		});
		createColumn("ID", DistrictSorter.ID_ASC, DistrictSorter.ID_DESC,
				tableViewer);
		createColumn("商品编号", DistrictSorter.ksid_asc, DistrictSorter.ksid_desc,
				tableViewer);
		createColumn("商品名称", DistrictSorter.ksarea_asc,
				DistrictSorter.ksarea_desc, tableViewer);
		createColumn("商品条码", DistrictSorter.ksarea_asc,
				DistrictSorter.ksarea_desc, tableViewer);
		createColumn("仓库编号", DistrictSorter.ksarea_asc,
				DistrictSorter.ksarea_desc, tableViewer);
		createColumn("单位", DistrictSorter.ksarea_asc,
				DistrictSorter.ksarea_desc, tableViewer);
		createColumn("库存数量", DistrictSorter.ksarea_asc,
				DistrictSorter.ksarea_desc, tableViewer);
		createColumn("库存金额", DistrictSorter.ksarea_asc,
				DistrictSorter.ksarea_desc, tableViewer);
		createColumn("库存均价", DistrictSorter.ksarea_asc,
				DistrictSorter.ksarea_desc, tableViewer);
		createColumn("零售金额", DistrictSorter.ksarea_asc,
				DistrictSorter.ksarea_desc, tableViewer);
		createColumn("库存差额", DistrictSorter.ksarea_asc,
				DistrictSorter.ksarea_desc, tableViewer);
		createColumn("库存上限", DistrictSorter.ksarea_asc,
				DistrictSorter.ksarea_desc, tableViewer);
		createColumn("库存下限", DistrictSorter.ksarea_asc,
				DistrictSorter.ksarea_desc, tableViewer);
		createColumn("规格", DistrictSorter.ksarea_asc,
				DistrictSorter.ksarea_desc, tableViewer);

		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		// initial the tabelViewer
		tableViewer.setContentProvider(new StorageTableContentProvider());
		tableViewer.setLabelProvider(new StorageTableLableProvider());

//		tableViewer.setInput(ExtentionInfo.getInstance().getStorageinfo());
		table.addListener(SWT.EraseItem, new SetTableColColorListener(
				table));
		table.select(0);
		table.setFocus();
		
		tabItem_1 = new CTabItem(tabFolder, SWT.NONE);
		tabItem_1.setText("库存往来");
		
		composite_2 = new Composite(tabFolder, SWT.NONE);
		tabItem_1.setControl(composite_2);
		composite_2.setLayout(new GridLayout(7, false));
		
		labelgoodsid = new Label(composite_2, SWT.NONE);
		labelgoodsid.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		labelgoodsid.setText("商品编号：");
		
		textgoodsid = new Text(composite_2, SWT.BORDER);
		textgoodsid.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		
		labelbarcode = new Label(composite_2, SWT.NONE);
		labelbarcode.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		labelbarcode.setText("商品条码：");
		
		textbarcode = new Text(composite_2, SWT.BORDER);
		textbarcode.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		
		label_1 = new Label(composite_2, SWT.NONE);
		label_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_1.setText("商品名称：");
		
		textgoodsname = new Text(composite_2, SWT.BORDER);
		textgoodsname.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		
		buttonsearch = new Button(composite_2, SWT.NONE);
		
		GridData gd_buttonsearch = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_buttonsearch.widthHint = 80;
		buttonsearch.setLayoutData(gd_buttonsearch);
		buttonsearch.setText("查询");
		
		iotableViewer = new TableViewer(composite_2, SWT.BORDER | SWT.FULL_SELECTION);
		iotableViewer.setUseHashlookup(true);
		iotable = iotableViewer.getTable();
		iotable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 7, 1));
		iotable.setToolTipText("点击单元格对数据进行修改！");
		// 设置行高
		iotable.addListener(SWT.MeasureItem, new Listener() {
			public void handleEvent(Event event) {

				event.height = 20;
			}
		});
		createColumn("ID", DistrictSorter.ID_ASC, DistrictSorter.ID_DESC,
				iotableViewer);
		createColumn("商品编号", DistrictSorter.ksid_asc, DistrictSorter.ksid_desc,
				iotableViewer);
		createColumn("商品名称", DistrictSorter.ksarea_asc,
				DistrictSorter.ksarea_desc, iotableViewer);
		createColumn("商品条码", DistrictSorter.ksarea_asc,
				DistrictSorter.ksarea_desc, iotableViewer);
		createColumn("单位", DistrictSorter.ksarea_asc,
				DistrictSorter.ksarea_desc, iotableViewer);
		createColumn("数量", DistrictSorter.ksarea_asc,
				DistrictSorter.ksarea_desc, iotableViewer);
		createColumn("金额", DistrictSorter.ksarea_asc,
				DistrictSorter.ksarea_desc, iotableViewer);
		createColumn("零售金额", DistrictSorter.ksarea_asc,
				DistrictSorter.ksarea_desc, iotableViewer);
		createColumn("出/入库日期", DistrictSorter.ksarea_asc,
				DistrictSorter.ksarea_desc, iotableViewer);
		createColumn("出/入库", DistrictSorter.ksarea_asc,
				DistrictSorter.ksarea_desc, iotableViewer);

		iotable.setHeaderVisible(true);
		iotable.setLinesVisible(true);

		// initial the tabelViewer
		iotableViewer.setContentProvider(new IostorageTableContentProvider());
		iotableViewer.setLabelProvider(new IostorageTableLableProvider());

//		iotableViewer.setInput(ExtentionInfo.getInstance().getIostorageinfo());
		iotable.addListener(SWT.EraseItem, new SetTableColColorListener(
				table));
		iotable.select(0);
		iotable.setFocus();
		
		tabFolder.setSelection(tabItem);
	}

	@Override
	public List getList() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void noticeFault(String message) {
		// TODO Auto-generated method stub

	}

	@Override
	public void noticeSuccess() {
		// TODO Auto-generated method stub

	}

	@Override
	public void addInfo() {
		// TODO Auto-generated method stub

	}

	@Override
	public void create() {
		// TODO Auto-generated method stub

	}

	@Override
	public void delete() {
		// TODO Auto-generated method stub

	}

	@Override
	public void print() {
		// TODO Auto-generated method stub

	}

	@Override
	public void refresh() {
		// TODO Auto-generated method stub

	}

	@Override
	public void save() {
		// TODO Auto-generated method stub

	}

	@Override
	public void search() {
		// TODO Auto-generated method stub

	}

	@Override
	public void searchByCondition() {
		// TODO Auto-generated method stub

	}

	@Override
	public void preprint() {
		// TODO Auto-generated method stub
		final PDocument doc = new PDocument("print");

		PTextBox t;

		t = new PTextBox(doc);

		t.setText("武汉立隆客商信息");

		new PVSpace(doc, 0.5);
		new PHLine(doc, 0.02, SWT.COLOR_RED);
		new PVSpace(doc, 0.5);

		// create the table
		SWTPTable swttable = new SWTPTable(doc);
		swttable.setTable(table);
		swttable.setBoxProvider(new PTableBoxProvider());
		
		PrintPreview pr = new PrintPreview(null, "信息打印", IconSource
				.getImage("print"), doc);
		pr.open();
	}
}
